<template>
  <div class="components-overview">
    <!-- 轮播 -->
    <div class="scroll-wrapper">
      <div class="scroll-con">
        <div
          v-for="(item, index) in systemTypeName"
          :key="index"
          :class="item.select?'item selected':'item'"
          @click="seleFlowOverview(item.id)"
        >
          <div class="item-data">
            <span>{{ item.name }}</span>
            <span>{{ $t('dataAnalysis.numberOfVisitors') }}</span>
          </div>
          <div class="item-data">
            <span class="number">{{ item.userNum }}</span>
          </div>
        </div>
      </div>
    </div>
    <!-- 轮播 -->

    <!-- 数据展示 -->
    <div class="show-data-wrap">
      <div class="show-data">
        <div class="data-board data-board-left">
          <div class="data-board__title visit-title">
            <div>
              <img
                src="~@/assets/img/data-analysis/visit.png"
                style="margin-bottom: 10px"
              >
              <p>{{ $t('dataAnalysis.pageBrowsing1') }}</p>
            </div>
          </div>
          <div class="data-board__content visit-content">
            <div class="data-board__content-item_box flex-box1">
              <div class="data-board__content-item">
                <div class="data-board__content-item_tittle">
                  {{ $t('dataAnalysis.numberOfVisitors') }}
                </div>
                <div class="num">
                  {{ dataArray.userNums }}
                </div>
                <div
                  v-if="props.dateType!==4"
                  class="compare"
                >
                  {{ typeName }}
                  <div
                    v-if="ratio"
                    :class="['lift-rate', ratio.userNumsRatioRise ===false ? 'down' : 'rise']"
                  >
                    <img
                      v-if="ratio.userNumsRatioRise===false"
                      src="~@/assets/icon/downArrow@2x.png"
                      class="arrows"
                    >
                    <img
                      v-else
                      src="~@/assets/icon/upArrow@2x.png"
                      class="arrows"
                    >
                    <span class="percentage">{{ Math.abs(ratio.userNumsRatio) }}%</span>
                  </div>
                </div>
              </div>

              <div
                v-if="props.dateType!==4"
                class="data-board__content-item"
              >
                <div class="data-board__content-item_tittle">
                  {{ $t('dataAnalysis.stayTime') }}
                </div>
                <div class="num">
                  {{ dataArray.averageStopTime }}
                </div>
                <div class="compare">
                  {{ typeName }}
                  <div
                    v-if="ratio"
                    :class="['lift-rate', ratio.averageStopTimeRatioRise===false ? 'down' : 'rise']"
                  >
                    <img
                      v-if="ratio.averageStopTimeRatioRise===false"
                      src="~@/assets/icon/downArrow@2x.png"
                      class="arrows"
                    >
                    <img
                      v-else
                      src="~@/assets/icon/upArrow@2x.png"
                      class="arrows"
                    >
                    <span class="percentage">{{ Math.abs(ratio.averageStopTimeRatio) }}%</span>
                  </div>
                </div>
              </div>
            </div>

            <div class="data-board__content-item_box flex-box2">
              <div
                v-if="props.dateType!==4"
                class="data-board__content-item"
              >
                <div class="data-board__content-item_tittle">
                  {{ $t('dataAnalysis.newVisitor') }}
                </div>
                <div class="num">
                  {{ dataArray.newUser }}
                </div>
                <div class="compare">
                  {{ typeName }}
                  <div
                    v-if="ratio"
                    :class="['lift-rate', ratio.newUserRatioRise===false ? 'down' : 'rise']"
                  >
                    <img
                      v-if="ratio.newUserRatioRise===false"
                      src="~@/assets/icon/downArrow@2x.png"
                      class="arrows"
                    >
                    <img
                      v-else
                      src="~@/assets/icon/upArrow@2x.png"
                      class="arrows"
                    >
                    <span class="percentage">{{ Math.abs(ratio.newUserRatio) }}%</span>
                  </div>
                </div>
              </div>

              <div class="data-board__content-item">
                <div class="data-board__content-item_tittle">
                  {{ $t('dataAnalysis.perVisits') }}
                </div>
                <div class="num">
                  {{ dataArray.averageVisitNums }}
                </div>
                <div
                  v-if="props.dateType!==4"
                  class="compare"
                >
                  {{ typeName }}
                  <div
                    v-if="ratio"
                    :class="['lift-rate', ratio.averageVisitNumsRatioRise===false ? 'down' : 'rise']"
                  >
                    <img
                      v-if="ratio.averageVisitNumsRatioRise===false"
                      src="~@/assets/icon/downArrow@2x.png"
                      class="arrows"
                    >
                    <img
                      v-else
                      src="~@/assets/icon/upArrow@2x.png"
                      class="arrows"
                    >
                    <span class="percentage">{{ Math.abs(ratio.averageVisitNumsRatio) }}%</span>
                  </div>
                </div>
              </div>
            </div>

            <div class="data-board__content-item_box flex-box3">
              <div class="data-board__content-item">
                <div class="data-board__content-item_tittle">
                  {{ $t('dataAnalysis.views') }}
                </div>
                <div class="num">
                  {{ dataArray.visitNums }}
                </div>
                <div
                  v-if="props.dateType!==4"
                  class="compare"
                >
                  {{ typeName }}
                  <div
                    v-if="ratio"
                    :class="['lift-rate', ratio.visitNumsRatioRise===false ? 'down' : 'rise']"
                  >
                    <img
                      v-if="ratio.visitNumsRatioRise===false"
                      src="~@/assets/icon/downArrow@2x.png"
                      class="arrows"
                    >
                    <img
                      v-else
                      src="~@/assets/icon/upArrow@2x.png"
                      class="arrows"
                    >
                    <span class="percentage">{{ Math.abs(ratio.visitNumsRatio) }}%</span>
                  </div>
                </div>
              </div>

              <div
                v-if="props.dateType!==4"
                class="data-board__content-item"
              >
                <div class="data-board__content-item_tittle">
                  {{ $t('dataAnalysis.lossRate') }}
                </div>
                <div class="num">
                  {{ dataArray.lossUserRate }}%
                </div>
                <div class="compare">
                  {{ typeName }}
                  <div
                    v-if="ratio"
                    :class="['lift-rate', ratio.lossUserRatioRise===false ? 'down' : 'rise']"
                  >
                    <img
                      v-if="ratio.lossUserRatioRise===false"
                      src="~@/assets/icon/downArrow@2x.png"
                      class="arrows"
                    >
                    <img
                      v-else
                      src="~@/assets/icon/upArrow@2x.png"
                      class="arrows"
                    >
                    <span class="percentage">{{ Math.abs(ratio.lossUserRatio) }}%</span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="data-board data-board-right">
          <div class="data-board__title deal-title">
            <div>
              <img
                src="~@/assets/img/data-analysis/deal.png"
                style="margin-bottom: 10px"
              >
              <p>{{ $t('dataAnalysis.dealTrans') }}</p>
            </div>
          </div>

          <div class="data-board__content deal-content">
            <div class="data-board__content-item">
              <div class="data-board__content-item_tittle">
                {{ $t('dataAnalysis.paidNumbers') }}
              </div>
              <div class="num">
                {{ dataArray.payUser }}
              </div>
              <div
                v-if="props.dateType!==4"
                class="compare"
              >
                {{ typeName }}
                <div
                  v-if="ratio"
                  :class="['lift-rate', ratio.payUserRatioRise===false ? 'down' : 'rise']"
                >
                  <img
                    v-if="ratio.payUserRatioRise===false"
                    src="~@/assets/icon/downArrow@2x.png"
                    class="arrows"
                  >
                  <img
                    v-else
                    src="~@/assets/icon/upArrow@2x.png"
                    class="arrows"
                  >
                  <span class="percentage">{{ Math.abs(ratio.payUserRatio) }}%</span>
                </div>
              </div>
            </div>
            <div
              v-if="props.dateType!==4"
              class="data-board__content-item"
            >
              <div class="data-board__content-item_tittle">
                {{ $t('dataAnalysis.vToP') }}
              </div>
              <div class="num">
                {{ dataArray.payRate }}%
              </div>
              <div class="compare">
                {{ typeName }}
                <div
                  v-if="ratio"
                  :class="['lift-rate', ratio.payRateRatioRise===false ? 'down' : 'rise']"
                >
                  <img
                    v-if="ratio.payRateRatioRise===false"
                    src="~@/assets/icon/downArrow@2x.png"
                    class="arrows"
                  >
                  <img
                    v-else
                    src="~@/assets/icon/upArrow@2x.png"
                    class="arrows"
                  >
                  <span class="percentage">{{ Math.abs(ratio.payRateRatio) }}%</span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 数据展示 -->
  </div>
</template>

<script setup>
import { reactive } from 'vue'

const systemTypeName = reactive([
  { name: $t('date.a'), id: 0, userNum: 0, select: true },
  { name: 'PC', id: 1, userNum: 0, select: false },
  { name: 'H5', id: 2, userNum: 0, select: false },
  { name: $t('sysManagement.miniPrograms'), id: 3, userNum: 0, select: false },
  { name: $t('dataAnalysis.android'), id: 4, userNum: 0, select: false },
  { name: 'IOS', id: 5, userNum: 0, select: false }
])

const typeName = $t('dataAnalysis.compareWithThePreviousOneDay')

const props = defineProps({
  sysType: { // 系统类型
    type: Number,
    default: 0
  },
  dateType: {
    type: Number,
    default: 0
  },
  dateRange: {
    type: Array,
    default: () => ([])
  }
})
const emit = defineEmits(['changeSysType'])

watch([() => props.dateRange, () => props.sysType], () => {
  getAnalysisData()
})

const ratio = ref({})
const ratioInit = () => {
  ratio.value = {
    averageStopTimeRatio: 0,
    averageStopTimeRatioRise: true,
    averageVisitNumsRatio: 0,
    averageVisitNumsRatioRise: true,
    lossUserRatio: 0,
    lossUserRatioRise: true,
    newUserRatio: 0,
    newUserRatioRise: true,
    payRateRatio: 0,
    payRateRatioRise: true,
    payUserRatio: 0,
    payUserRatioRise: true,
    userNumsRatio: 0,
    userNumsRatioRise: true,
    visitNumsRatio: 0,
    visitNumsRatioRise: true
  }
}

// 请求接口
const dataArray = ref({
  userNums: 0,
  newUser: 0,
  visitNums: 0,
  averageStopTime: 0,
  averageVisitNums: 0,
  lossUserRate: '0.00%',
  payUser: 0,
  payRate: '0.00%'
})
const getAnalysisData = () => {
  let startTime = ''
  let endTime = ''
  if (props.dateRange.length > 0) {
    startTime = props.dateRange[0]
    endTime = props.dateRange[1]
  }
  ratioInit()
  http({
    url: http.adornUrl('/platform/flowAnalysis/getAnalysisData'),
    method: 'get',
    params: http.adornParams({
      timeType: props.dateType,
      start: startTime,
      end: endTime,
      systemType: props.sysType
    })
  }).then(({ data }) => {
    if (data) {
      dataArray.value = data
      ratio.value = data.ratio
    }
  })

  // 请求第一部分所有系统的访客数
  http({
    url: http.adornUrl('/platform/flowAnalysis/systemTypeNums'),
    method: 'get',
    params: http.adornParams({
      timeType: props.dateType,
      start: startTime,
      end: endTime
    })
  }).then(({ data }) => {
    if (data) {
      for (let i = 0; i < systemTypeName.length; i++) {
        if (i === 0) {
          systemTypeName[i].userNum = data.all
        } else if (i === 1) {
          systemTypeName[i].userNum = data.pc
        } else if (i === 2) {
          systemTypeName[i].userNum = data.h5
        } else if (i === 3) {
          systemTypeName[i].userNum = data.applets
        } else if (i === 4) {
          systemTypeName[i].userNum = data.android
        } else if (i === 5) {
          systemTypeName[i].userNum = data.ios
        }
        systemTypeName[i].select = props.sysType === i
      }
    }
  })
}
// 流量总览点击切换
const seleFlowOverview = (systemType) => {
  emit('changeSysType', systemType)
}

</script>

<style lang="scss" scoped>
.components-overview {
  /* 轮播 */
  .scroll-con {
    white-space: nowrap;
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    margin-top: 25px;
    .item {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      height: 92px;
      width: 16%;
      border: 1px solid #dcdee0;
      border-radius: 4px;
      padding: 9px 16px 11px 16px;
      box-sizing: border-box;
      cursor: pointer;
      transition: box-shadow 0.2s ease 0s;
      &:last-child {
        margin-right: 0;
      }
      &:hover {
        box-shadow: 0 2px 8px rgba(200, 201, 204, 0.5);
      }
    }
    .selected {
      background-color: #edf4ff;
      border-color: #155bd4;
    }
  }
  .item-data {
    display: flex;
    -ms-flex-pack: justify;
    justify-content: space-between;
    .number {
      font-size: 25px;
      font-weight: bold;
      color: #333333;
    }
  }
  /* 轮播 */
  /* 数据展示 */
  .show-data-wrap {
    margin-top: 30px;
    width: 100%;
    overflow-x: auto;
    .show-data {
      display: flex;
      min-width: 1355px;
      .data-board {
        display: flex;
        margin-bottom: 4px;
        &.data-board-left {
          width:60%;
          margin-right:10px
        }
        &.data-board-right {
          width:40%
        }
      }
    }
  }
  .flex-box1{
    flex: 2;
  }
  .flex-box2{
    flex: 4;
    align-items: center;
    .data-board__content-item {
      padding-left: 0;
      margin-left: -3%;
    }
  }
  .flex-box3{
    flex: 2;
  }

  .data-board__content-item_tittle {
    font-size: 14px;
    font-weight: 400;
    color: #333333;
  }

  .data-board {
    .data-board__title {
      padding: 0 12px;
      display: -ms-flexbox;
      display: flex;
      -ms-flex-align: center;
      align-items: center;
      -ms-flex-pack: center;
      justify-content: center;
      color: #fff;
      text-align: center;
      -ms-flex: none;
      flex: none;
    }
    .visit-title {
      background-color: #155bd4;
    }
    .visit-content {
      background-color: rgb(243, 247, 253);
    }
    .deal-title {
      background-color: #155bd4;
    }
    .deal-content {
      background-color: #f3f7fd;
    }
    .data-board__content {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
      flex: 1 1 auto;
      padding: 0 8px;
    }
  }
  .data-board__content-item_box {
    display: flex;
    flex-direction: column;
  }
  .data-board__content-item {
    padding: 35px;
    .num {
      font-size: 18px;
      font-weight: bold;
      color: #333333;
      opacity: 1;
      margin-top: 9px;
      margin-bottom: 12px;
    }
    .compare {
      color: #969799;
      margin-top: 8px;
      display: flex;
      // 新增箭头
      .lift-rate {
        display: flex;
        align-items: center;
        font-size: 12px;
        margin-left: 16px;
        .arrows {
          display: block;
          width: 8px;
          height: 10px;
          margin-right: 3px;
        }
        &.down{
          .percentage {
            color: #FF4141;
            line-height: 1em;
          }
        }
        &.rise{
          .percentage {
            color: #3CC480;
            line-height: 1em;
          }
        }
      }
    }
  }
  /* 数据展示 */
}

</style>
